package com.czk.utlls;

import javax.net.ssl.HttpsURLConnection;
import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

/**
 * @Author:ChenZhangKun
 * @Date: 2021/8/12 15:19
 */
public class HttpUtil {
    /**
     * 发送get请求
     *
     * @param address
     * @return
     */
    public static String get(String address) {
        try {
            URL url = new URL(address);
            URLConnection urlConnection = url.openConnection();
            // 定义
            InputStream inputStream = urlConnection.getInputStream();
            int len = 0;
            byte[] b = new byte[1024];
            StringBuilder builder = new StringBuilder();
            while ((len = inputStream.read(b)) != -1) {
                builder.append(new String(b, 0, len));
            }
            return builder.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 上传图片接口
     *
     * @param urlPath
     * @param path
     * @return
     */
    public static String uploadPic(String urlPath, String path) {
        File file = new File(path);
        try {
            URL url = new URL(urlPath);
            // 强转为安全连接
            HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection();
            //URLConnection urlConnection = url.openConnection();
            // 设置连接的信息
            urlConnection.setDoInput(true);
            urlConnection.setDoOutput(true);
            // 不适用缓存
            urlConnection.setUseCaches(false);
            // 长连接
            urlConnection.setRequestProperty("connection", "keep-alive");
            urlConnection.setRequestProperty("Charset", "utf-8");
            String boundary = System.currentTimeMillis() + "-----";
            // 数据的边界
            urlConnection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
            // 准备数据
            // 第一部分，头部信息
            StringBuilder builder = new StringBuilder();
            builder.append("--");
            builder.append(boundary);
            builder.append("\r\n");
            builder.append("Content-Disposition:form-data;name=\"media\";filename=\" " + file.getName() + "\"\r\n");
            builder.append("Content-Type:application/octet-stream\r\n\r\n");
            // 获取输出流
            OutputStream outputStream = urlConnection.getOutputStream();
            outputStream.write(builder.toString().getBytes());
            // 第二部分，文件内容
            byte[] b = new byte[1024];
            int len = 0;
            InputStream inputStream = new FileInputStream(file);
            while ((len = inputStream.read(b)) != -1) {
                outputStream.write(b, 0, len);
            }
            // 第三部分， 尾部信息
            String foot = "\r\n--" + boundary + "--\r\n";
            outputStream.write(foot.getBytes());
            outputStream.flush();
            outputStream.close();
            // 读数据
            InputStream inputStream1 = urlConnection.getInputStream();
            StringBuilder resp = new StringBuilder();

            while ((len = inputStream1.read(b)) != -1) {
                resp.append(new String(b, 0, len));
            }
            return resp.toString();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
